//
//  PostImageCell.swift
//  WeiboDemo
//
//  Created by 姬向阳 on 2020/4/19.
//  Copyright © 2020 SUNNYJ. All rights reserved.
//

import SwiftUI

// 定义常量 图片间距
private let kImageSpace : CGFloat = 6

// 图片Cell
struct PostImageCell: View {
    let images : [String] // 图片Cell需要显示的图片数组
    let width : CGFloat // 图片Cell需要显示的宽度
    
    var body: some View {
        Group{
            
            if images.count == 1 {
                loadImage(name: images[0])
                    .resizable()
                    .scaledToFill()
                    .frame(width : width , height: width * 0.75 )
                    .clipped()
            }else if images.count == 2 {
                PostImageCellRow(images: images, width: width)
            }else if images.count == 3 {
                PostImageCellRow(images: images, width: width)
            }else if images.count == 4 {
                VStack (spacing: kImageSpace) {
                    PostImageCellRow(images: Array(images[0...1]), width: width)
                    PostImageCellRow(images: Array(images[2...3]), width: width)
                }
            }else if images.count == 5 {
                VStack (spacing: kImageSpace) {
                    PostImageCellRow(images: Array(images[0...2]), width: width)
                    PostImageCellRow(images: Array(images[3...4]), width: width)
                }
            }else if images.count == 6 {
                VStack (spacing: kImageSpace) {
                    PostImageCellRow(images: Array(images[0...2]), width: width)
                    PostImageCellRow(images: Array(images[3...5]), width: width)
                }
            }
            
        }
    }
}

// 私有View 图片Cell的一行图片
private struct PostImageCellRow : View{
    let images : [String] // 该行需要显示的图片数组
    let width : CGFloat // 该行需要显示的宽度
    
    var body :some View {
        // 用HStack包裹
        HStack (spacing: 6) {
            ForEach(images, id: \.self) { image in
                // 每张图片都调用一次loadImage返回图片
                loadImage(name: image)
                    .resizable()
                    .scaledToFill()
                    .frame(
                        width: (self.width - kImageSpace * CGFloat(self.images.count - 1)) / CGFloat(self.images.count) ,
                        height: (self.width - kImageSpace * CGFloat(self.images.count - 1)) / CGFloat(self.images.count)
                )
                    .clipped()
            }
        }
    }
}

// 预览
struct PostImageCell_Previews: PreviewProvider {
    static var previews: some View {
        let images = UserData().recommendPostList.list[0].images // 定义了JSON中第一个post的图片数组
        let width = UIScreen.main.bounds.width - 30 // 设置显示宽度为屏幕宽度减去30
        return Group {
            // 使用Group包裹返回多个View,达到一次预览多个View的效果.每个View输入不同的数组大小
            PostImageCell(images: Array(images[0...0]),width: width)
            PostImageCell(images: Array(images[0...1]),width: width)
            PostImageCell(images: Array(images[0...2]),width: width)
            PostImageCell(images: Array(images[0...3]),width: width)
            PostImageCell(images: Array(images[0...4]),width: width)
            PostImageCell(images: Array(images[0...5]),width: width)
        }
        .previewLayout(.fixed(width: width, height: 400)) // 修改预览界面的大小
    }
}
